//
//  SCLoginViewController.m
//  Assembly
//
//  Created by 葛悦彣 on 2023/11/27.
//

#import "SCLoginViewController.h"
#import "SCBaseTabBarViewController.h"
#import "AppDelegate.h"
#import "YTForgotPasswordViewController.h"

@interface SCLoginViewController ()<UITextFieldDelegate>

@property (nonatomic ,strong) UIView * phoneView;
@property (nonatomic ,strong) UIView * passWordView;
@property (nonatomic ,strong) UITextField * phoneText;
@property (nonatomic ,strong) UIButton * delBtn;
@property (nonatomic ,strong) UITextField * passWordText;

@property (nonatomic ,strong) UIButton * getCodeBtn;
@property (nonatomic ,strong) UIButton *loginBtn;
@property (nonatomic ,assign) NSInteger timeOut;
@property (nonatomic ,assign) UIButton * mmycBtn;

@end

@implementation SCLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}


-(void)initUI{
    UIImageView *bgImage = [UIImageView new];
    [self.view addSubview:bgImage];
    bgImage.image = [UIImage imageNamed:@"top_bg"];
    [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(kwt(173));
        make.top.offset(0);
    }];
    
    UIView * topbjview = [UIView new];
    topbjview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(10) andView:topbjview];
    [self.view addSubview:topbjview];
    [topbjview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.top.offset(kwt(136));
        make.bottom.offset(0);
    }];
    
    
    UIView *phoneView = [UIView new];
    self.phoneView = phoneView;
    [self.view addSubview:phoneView];
    phoneView.backgroundColor =  KcolorHex(@"#F7F7F7");
    [DTKTool YJBK:kwt(5) andView:phoneView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(160));
        make.left.offset(kwt(16));
        make.right.offset(kwt(-16));
        make.height.offset(kwt(50));
    }];
    
    UIImageView *phoneImage = [UIImageView new];
    [phoneView addSubview:phoneImage];
    phoneImage.image = [UIImage imageNamed:@"account_number"];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(20));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));

    }];
        
    self.phoneText = [UITextField new];
    [phoneView addSubview:_phoneText];
    _phoneText.delegate = self;
    _phoneText.font = kFont(17);
    _phoneText.textColor = KcolorHex(@"#1E2425");
    _phoneText.placeholder = @"请输入手机号";
    _phoneText.keyboardType = UIKeyboardTypeNumberPad;
    [_phoneText addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(50));
        make.centerY.offset(0);
        make.width.offset(kwt(250));
    }];
    
    self.delBtn = [UIButton new];
    self.delBtn.hidden = YES;
    [phoneView addSubview:_delBtn];
    [_delBtn setImage:[UIImage imageNamed:@"search_delete"] forState:UIControlStateNormal];
    [_delBtn addTarget:self action:@selector(delClick) forControlEvents:UIControlEventTouchDown];
    [_delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-20));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));

    }];
    
    UIView *passWordView = [UIView new];
    self.passWordView = passWordView;
    [self.view addSubview:passWordView];
    passWordView.backgroundColor = KcolorHex(@"#F7F7F7");
    [DTKTool YJBK:kwt(5) andView:passWordView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [passWordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneView.mas_bottom).offset(kwt(18));
        make.left.offset(kwt(16));
        make.right.offset(kwt(-16));
        make.height.offset(kwt(50));
    }];
    
    UIImageView *pwdImage = [UIImageView new];
    [passWordView addSubview:pwdImage];
    pwdImage.image = [UIImage imageNamed:@"password"];
    [pwdImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(20));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));

    }];
    
    UITextField * passWordText = [UITextField new];
    self.passWordText = passWordText;
    [passWordView addSubview:passWordText];
    passWordText.delegate = self;
    passWordText.font = kFont(17);
    passWordText.textColor = KcolorHex(@"#1E2425");
    passWordText.placeholder = @"请输入密码";
//    passWordText.secureTextEntry = YES;
//    passWordText.keyboardType = UIKeyboardTypeNumberPad;
    [passWordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(50));
        make.centerY.offset(0);
        make.width.offset(kwt(250));
    }];
    self.passWordText.secureTextEntry = YES;
    
    UIButton * mmycBtn = [UIButton new];
    self.mmycBtn = mmycBtn;
    self.mmycBtn.hidden = YES;
    [passWordView addSubview:mmycBtn];
    [mmycBtn setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    [mmycBtn addTarget:self action:@selector(mmycClick) forControlEvents:UIControlEventTouchDown];
    [mmycBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-20));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));
    }];
    

    UIButton *loginBtn = [UIButton new];
    [self.view addSubview:loginBtn];
    loginBtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(345), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = kCFont(17);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.layer.cornerRadius = kwt(5);
    [loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchDown];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passWordView.mas_bottom).offset(kwt(24));
        make.width.offset(kwt(345));
        make.centerX.offset(0);
        make.height.offset(kwt(50));
    }];
    
    
    UIButton * wjBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [wjBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [wjBtn addTarget:self action:@selector(findPwdClick) forControlEvents:UIControlEventTouchUpInside];
    [wjBtn setTitleColor:KcolorHex(@"#515151") forState:UIControlStateNormal];
    wjBtn.titleLabel.font = kFont(16);
    [self.view addSubview:wjBtn];
    [wjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-17));
        make.top.equalTo(loginBtn.mas_bottom).offset(kwt(16));
        make.height.offset(kwt(20));
        //        make.width.offset(kwt(80));
    }];
    
    
}


- (void)findPwdClick {
    YTForgotPasswordViewController *page = [YTForgotPasswordViewController new];
//    page.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.navigationController pushViewController:page  animated:YES];
}

///船修登录
- (void)loginNet{
    [self.view showBusyHUD];
    [SCNetManger PostAppAppLoginWithDict:@{ @"account": self.phoneText.text,
                                            @"password":self.passWordText.text,
                                         } andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if (model==nil) {
            [self.view showWarning:@"网络不可用"];
        }else{
            if (model.code == 200){
                UserInfoModel * usermodel = [UserInfoModel yy_modelWithJSON:model.result];
                
                [UserInfoModel shared].token = usermodel.token;
                [UserInfoModel shared].username = usermodel.username;
                [UserInfoModel shared].phone = usermodel.phone;
                [UserInfoModel shared].realname = usermodel.realname;
                [UserInfoModel shared].OrgCode = usermodel.userInfo.appOrgCode;
                [UserInfoModel shared].userIdentity = usermodel.userInfo.userIdentity;
                [UserInfoModel shared].userID = usermodel.userInfo.ID;
                [self pushIdWithToken:usermodel.token];
                AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                SCBaseTabBarViewController * main = [[SCBaseTabBarViewController alloc]init];
                delegate.window.rootViewController = main;
             
            }else{
                [self.view showWarning:model.message];

            }
        }
    }];
}

#pragma mark - 写入极光推送id
-(void)pushIdWithToken:(NSString *)token{
    NSUserDefaults *userDefauts  = [NSUserDefaults standardUserDefaults];

    [SCNetManger sysSmsAppUpDateRegistrationIdWithDict:@{ @"registrationId": [userDefauts valueForKey:@"APNSJGID"]} andCompetionHandlerBlock:^(HTTPDicBase * model) {
//        if (model==nil) {
//            [self.view showWarning:@"网络不可用"];
//        }else{
//            
//                [self.view showWarning:model.msg];            
//        }
    }];
}
-(void)loginClick{
    if ([self.phoneText.text isEqualToString:@""]) {
        [self.view showWarning:@"请输入账号"];
    }else if (self.phoneText.text.length != 11){
        [self.view showWarning:@"请输入本人11位手机号码！"];
    }else if ([self.passWordText.text isEqualToString:@""]){
        [self.view showWarning:@"请输入密码"];
    }else{
        [self loginNet];
    }
}

- (void)textFieldChange:(UITextField *)textField {
    if(self.phoneText == textField){
        self.delBtn.hidden = (textField.text.length == 0);
    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if(self.phoneText == textField){
        self.phoneView.backgroundColor = UIColor.whiteColor;
        self.passWordView.backgroundColor = KcolorHex(@"#F7F7F7");
        [DTKTool YJBK:kwt(5) andView:self.phoneView andWidth:1 andColor:KcolorHex(@"#2A79FC")];
        [DTKTool YJBK:kwt(5) andView:self.passWordView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];

    }
    if(self.passWordText == textField){
        self.passWordView.backgroundColor = UIColor.whiteColor;
        self.phoneView.backgroundColor = KcolorHex(@"#F7F7F7");
        [DTKTool YJBK:kwt(5) andView:self.passWordView andWidth:1 andColor:KcolorHex(@"#2A79FC")];
        [DTKTool YJBK:kwt(5) andView:self.phoneView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
        self.mmycBtn.hidden = NO;
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    self.phoneView.backgroundColor = KcolorHex(@"#F7F7F7");
    self.passWordView.backgroundColor = KcolorHex(@"#F7F7F7");
    [DTKTool YJBK:kwt(5) andView:self.phoneView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    [DTKTool YJBK:kwt(5) andView:self.passWordView andWidth:1 andColor:KcolorHex(@"#E0E0E0")];
    self.mmycBtn.hidden = (self.passWordText.text.length < 1);

}
- (void)mmycClick{
    if(self.passWordText.secureTextEntry){
        [self.mmycBtn setImage:[UIImage imageNamed:@"look"] forState:UIControlStateNormal];
    } else {
        [self.mmycBtn setImage:[UIImage imageNamed:@"no_look"] forState:UIControlStateNormal];
    }
    self.passWordText.secureTextEntry = !self.passWordText.secureTextEntry;
}
- (void)delClick{
    self.delBtn.hidden = YES;
    self.phoneText.text = @"";
    
}
@end
